<?php

function tmplHelloWorld($tmpl) {
    return 'hello world';
}

if (!function_exists('tmpl__')) {

    function tmpl__($tmpl, $langKey) {
        return Kd_Localize_Helper::__($langKey);
    }

}

if (!function_exists('tmplGetUrl')) {

    function tmplGetUrl($tmpl, $arguments = array(), $action = '*', $controller = '*', $component = '*') {
        return Kd_Web::getRouter()->buildUrl("$component/$controller/$action", $arguments);
    }

}

if (!function_exists('tmplRewriteUrl')) {

    function tmplRewriteUrl($tmpl, $arguments = array(), $action = '*', $controller = '*', $component = '*') {
        $result = Kd_Web::getRouter()->buildUrl("$component/$controller/$action", $arguments);
        return '{{rewrite "'.$result.'"}}';
    }

}

if (!function_exists('tmplLoadTemplate')) {

    function tmplLoadTemplate($tmpl, $subPath, array $arguments = array()) {
        $path = Kd_Util_IO::findFile($tmpl->getDomain()->getConfig()->getSetting('templateDirectories'), 
        									$subPath);
        $result = new Dcmp_Template($tmpl->getDomain());
        return $result->assignMulti($arguments)
                      ->render($path);
    }

}

if (!function_exists('tmplGetComponentSkinUrl')) {

    function tmplGetComponentSkinUrl($tmpl, $subpath, array $arguments = array()) {
        $var = Kd_Web::getVariables();
        $path = $var->getExtensionSkinPaths();
        if (file_exists(BP . $path . 'component' . DS . $subpath)) {
            return str_replace(DS, '/' . $var->getPubUrl() . $path . 'component' . DS . $subpath);
        }
    }

}

if (!function_exists('tmplLoadModule')) {

    function tmplLoadModule($tmpl, $name, $arguments = array()) {
        $result = App::getBlock($name,$tmpl->getDomain());
        if ($result) {
            return $result->render($arguments);
        }
    }

}

if (!function_exists('tmplLoadBlock')) {

    function tmplLoadBlock($tmpl, $position) {
        $query = App::getModel('design/collection_block')
                    ->joinBlockTemplate()
                    ->joinModule()
                    ->joinPageTemplate()
                    ->where()
                        ->group()
                            ->eq('a.page_template_id', ':templateId2:s')
                            ->eq('a.position', ':position:s')
                    ->closest()
                    ->addParameter('templateId2', '00000000000000000000000000000000')
                    ->addParameter('position', $position)
                    ->sortBy('sorting','asc');
        if(Kd_Web::getVariables()->issetData('page_template')){
            $query->addParameter('templateId1', Kd_Web::getVariables()->getPageTemplate()->getId());
            $query->where()->orNext()->group()
                            ->eq('a.page_template_id', ':templateId1:s')
                            ->eq('a.position', ':position:s')
                        ->endSelf();
        }
        $blocks = $query->load();
        $result = '';
        foreach ($blocks as $block) {
            $module = App::getModule($block->getDomain() . '/' . $block->getModuleType(), $tmpl->getDomain());
            $module->setParameters($block['parameters'])->setBlock($block);
            $id = !empty($block['parameters']['id_prefix'])?
                    ' id = "' . $block['parameters']['id_prefix'] . '"':
                    '';
            $class = !empty($block['parameters']['css_prefix'])?
                        ' class="'.$block['parameters']['css_prefix'].'"':
                        '';
            $html = $module->render();
            if(strlen(trim($html)) > 0){
                $result .= '<div' . $class . $id . '>' . $html . '</div>';
            }
            
        }
        
        return $result;
    }
}


if (!function_exists('tmpGetModuleByPosition')) {

    function tmplGetModuleByPosition($tmpl, $position, $arguments = array()) {
        $result = '';
        $templateObject = Kd_Web::getVariables()->getTemplateObject();
        foreach ($templateObject->getModules($position) as $module) {
            $result .= App::getBlock($module['module_name'])->render($module->getParameters());
        }
        return $result;
    }

}

if (!function_exists('tmplGetStorefrontPicture')) {
    function tmplGetStorefrontPicture($tmpl, $roadid, $picture) {
        return Kd_Web::getVariables()->getPubUrl().'media/poi/'.$roadid.'/poi/'.$picture.'.jpg';
    }
}

if (!function_exists('tmplFormatDate')) {
    function tmplFormatDate($tmpl, $date = NULL, $format = 's') {
        $time = strtotime($date);
        switch($format){
            case 's':
                return date('Y-m-d', $time);
            case 'l':
                return date('Y-m-d H:i:s', $time);
            case 'sf':
                return date('Y年m月d日', $time);
            case 'lf':
                return date('Y年m月d日H时i分s秒', $time);
            default:
                return date($format, $time);
        }
    }
}

if(!function_exists('tmplValidateCodeImage')){
    function tmplValidateCodeImage($tmpl){
        return '<img onclick="javascript:this.src=this.src+1" src="'.Kd_Web::getRouter()->getBaseUrl().'validatecode.php?" width="130" height="50" style="cursor:pointer" alt="点击刷新"/>';
    }
}